package com.itheima.reggie.filter;

import com.alibaba.fastjson.JSON;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@WebFilter("/*")
public class LoginChekFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //强转请求类型
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //获取本次请求的路径
        String requestURI = request.getRequestURI();

        //过滤访问地址
        StringBuffer requestURL = request.getRequestURL();
        String name = request.getServerName();
        /*System.out.println(name);
        System.out.println(requestURI);
        System.out.println(requestURL);*/
        /*if (!(requestURL.indexOf("localhost")>=0)) {
            response.getWriter().write("滚犊子");
            return;
        }*/

        //定义需要放行的路径
        String[] uris = new String[]{
                "/backend/**",
                "/front/**",
                "/employee/login",
                "/employee/logout",
                "/user/sendMsg",
                "/user/login"
        };
        //定义一个方法去判断路径是否匹配,是否需要放行
        boolean b = chek(uris, requestURI);
        if (b) {
            filterChain.doFilter(request, response);
            return;
        }
        //4、判断登录状态，如果已登录，则直接放行
        Long empId = (Long) request.getSession().getAttribute("employee");
        if (empId != null) {
            //将登陆用户ID存入线程中
            BaseContext.setId(empId);

            filterChain.doFilter(request, response);
            return;
        }
        //增加判断移动端登录状态，如果已登录，则直接放行
        Long userId = (Long) request.getSession().getAttribute("user");
        if (userId != null) {
            BaseContext.setId(userId);
            filterChain.doFilter(request, response);
            return;
        }
        //5、如果未登录则返回未登录结果，通过输出流方式向客户端页面响应数据
        R r = R.error("NOTLOGIN");
        String s = JSON.toJSONString(r);
        response.getWriter().write(s);

    }

    //创建一个路径的匹配器，支持带通配符的比较
    AntPathMatcher PathMatcher = new AntPathMatcher();

    private boolean chek(String[] uris, String requestURI) {

        for (String s : uris) {
            boolean b = PathMatcher.match(s, requestURI);
            if (b) {
                //比较相同返回true
                return true;
            }
        }
        return false;
    }


    @Override
    public void destroy() {
        Filter.super.destroy();
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }
}
